package controllers

import "myerp/models"
import "myerp/src"
import "time"
import "fmt"

//采购
type PurchaseController struct {
	CommonController
}

//添加
func (c *PurchaseController) Add() {
	if c.Ctx.Request.Method == "POST" {
		reData := make(map[string]interface{})
		reData["status"] = 0
		reData["msg"] = "添加失败！"
		var name, cname, phone string
		var ifpay, cid int
		var zprice float64
		var err error
		var md models.Purchase
		md.Goods = c.GetStrings("goods[]")
		fmt.Println(md.Goods)
		md.GoodsNum = c.GetStrings("GoodsNum[]")

		if name = c.GetString("name"); name == "" {
			reData["msg"] = "采购主题不能为空"
			c.Data["json"] = reData
			c.ServeJSON()
			return
		}
		if cname = c.GetString("name"); cname == "" {
			reData["msg"] = "联系人不能为空"
			c.Data["json"] = reData
			c.ServeJSON()
			return
		}
		if phone = c.GetString("name"); phone == "" {
			reData["msg"] = "联系电话不能为空"
			c.Data["json"] = reData
			c.ServeJSON()
			return
		}
		if cid, err = c.GetInt("cid"); err != nil {
			reData["msg"] = "采购商不存在"
			c.Data["json"] = reData
			c.ServeJSON()
			return
		}
		ifpay, _ = c.GetInt("ifpay", 0)
		if zprice, err = c.GetFloat("zprice"); err != nil {
			reData["msg"] = "参数不正确，JS没有加载完成"
			c.Data["json"] = reData
			c.ServeJSON()
			return
		}

		md.Cid = cid
		md.Ifpay = ifpay
		md.Uid = int(UserInfo.Id)
		md.Number = src.OrderNumber("C")
		md.Ctime = int(time.Now().Unix())
		md.Cname = cname
		md.Phone = phone
		md.Zprice = zprice
		md.Name = name
		_, err = models.AddPurchase(&md)
		if err != nil {
			reData["msg"] = "参数不正确，JS没有加载完成"
		} else {
			c.DelSession("purchaseorder")
			reData["status"] = 1
			reData["url"] = "/purchase/list"
		}
		c.Data["json"] = reData
		c.ServeJSON()
		return

	}

	//获取商品
	gid := c.GetSession("purchaseorder")
	if gid == nil {

		c.Data["message"] = "你没有选择采购的商品"
		c.Data["uri"] = "/purchase/list"
		c.Data["st"] = 0
		c.TplName = "error.html"
		return
	}
	_, gl := models.GetGoodsList(1, 1000, "my_goods.id in("+gid.(string)+")")
	c.Data["glist"] = gl
	//获取供应商
	l, _ := models.GetClientsByCtype()
	c.Data["cl"] = l
	c.Data["ActionName"] = "添加采购单"
	c.Layout = "base.html"
	c.TplName = "purchase/add.html"
}

//编辑
func (c *PurchaseController) Edite() {
	var id int
	var err error
	if c.Ctx.Request.Method == "POST" {
		reData := make(map[string]interface{})
		id, err = c.GetInt("id")
		cname := c.GetString("cname")
		phone := c.GetString("phone")
		ifpay, err := c.GetInt("ifpay", 0)
		var md models.Purchase
		md.Id = id
		md.Cname = cname
		md.Phone = phone
		md.Ifpay = ifpay
		err = models.UpdatePurchaseById(&md)
		if err != nil {
			reData["status"] = 0
			reData["msg"] = "修改失败！"
		} else {
			reData["status"] = 1
			reData["url"] = "purchase/list"
		}
		c.Data["json"] = reData
		c.ServeJSON()
		return

	}
	if id, err = c.GetInt("id"); err != nil {
		c.Redirect("purchase/list", 302)
		return
	}
	//获取采购单信息
	infos, err := models.GetPurchaseById(id)
	if err != nil {
		c.Redirect("purchase/list", 302)
		return
	}
	c.Data["infos"] = infos
	//获取商品信息  稍后写页面的时候在详细写

	c.Data["ActionName"] = "编辑采购单"
	c.Layout = "base.html"
	c.TplName = "purchase/edite.html"
}

//列表
func (c *PurchaseController) List() {
	//稍后添加具体列表
	c.Data["ActionName"] = "采购管理"
	page, _ := c.GetInt("page", 1)
	count, l := models.GetPurchaseList(int64(page), 10, "")
	c.Data["List"] = l
	var pages src.Page = src.NewPage(page, 10, int(count), "/purchase/list")
	c.Data["page"] = pages.Show()
	c.Layout = "base.html"
	c.TplName = "purchase/list.html"
}

//快速修改状态
func (c *PurchaseController) Qstatus() {
	reData := make(map[string]interface{})
	id, _ := c.GetInt("id", 0)
	if id <= 0 {
		reData["status"] = 0
		reData["msg"] = "修改失败"
		c.Data["json"] = reData
		c.ServeJSON()
		return
	}
	err := models.UpdatePurchaseIfpayById(id)
	if err != nil {
		reData["status"] = 0
		reData["msg"] = "修改失败"
	} else {
		reData["status"] = 1
		reData["msg"] = "修改成功"
	}
	c.Data["json"] = reData
	c.ServeJSON()
	return
}
